String Functions


Description
 


C
onvert a string variable to numerical variable.

Code Examples
 

** Convert mystring$ variable to integer value and store it in RetVal
mystring$=
'1.222'
RetVal=VAL(mystring$)

** Convert string variables and count them
mystring_A$=
'20'
mystring_B$='10'
RetVal=VAL(mystring_A$) + VAL(mystring_B$)

Additional Info
 

For additional information about VAL check this link.

 

Description
 


Convert a numerical variable to string

This type of conversion (numbers to text) is used most often. Here's an example of displaying advanced string with data retrieved from numerical variable:

Code Examples
 

NumOfYears=36
UserAge$
=
CHAR(NumOfYears)+' years old'
Message("Our user is ","UserAge$")

Result of these code lines will be displayed in message box:

Our user is 36 years old

Additional Info
 

For additional information about CHAR check this link.

 

Description
 


Returns the character (from ASCII table) with the specified ordinal (decimal) value (0-255).

Complete ASCII character table can be found here.

Code Examples
 

string$=CHR(169) + ' Odklizec 2003'
LoadText("Text","string$")

Result of these code lines will be displayed in Text object:

⌐ Odklizec 2003

string$= 'First line' + CHR(13) + CHR(10) + 'Second line'
LoadText("Text","string$")

Result of these code lines will be text divided into two lines:

First line
Second line

 

Description
 


Returns the ordinal (decimal) value of the specified character.

Complete ASCII character table can be found here.

Code Examples
 

character$='#'
RetVal=
ORD(character$)

Result of this code will be 35

 

Description
 


Returns the length of the specified string.

Code Examples
 

LoadText("string$","c:\test.txt")
RetVal=
LEN(string$)

Result of this code will be length of the loaded file.

 

Description
 


Returns a string with the same text as the string passed in String$ variable, but with all letters converted to LOWercase/UPPercase.

Code Examples
 

string$='ThIs TeXt WiLL bE CoNvErtED to LoWeRcAsE'
RetString$=
LOW(string$)

Result of this code will be text converted to lowercase.

    this text will be converted to lowercase

string$='ThIs TeXt WiLL bE CoNvErtED to UppErCasE'
RetString$=
UPP(string$)

Result of this code will be text converted to lowercase.

    THIS TEXT WILL BE CONVERTED TO UPPERCASE

 

POS(SubString$, String$)

Description
 


Searches for SubString within String and returns an integer value (starting from 1) that is the index of the first character of SubString within String.

If Substr is not found, POS returns zero.

Code Examples
 

string$='Have a nice day!'
substring$=
'nice'
RetVal=
POS(substring$,string$)

Result of the above code will be 8.

Additional Info
 

Because POS function (as well as other string functions) is case sensitive, we recommend you to convert the source string to upper or lowercase before starting POS search.

string$='Have a nice day!'
substring$=
'have'
RetString$=LOW(string$)
RetVal=
POS(substring$,RetString$)

If you don't convert the source string$ to Lowercase, then POS returns 0, because 'Have'<>'have'. But after converting string$ to Lowercase POS returns 1.

 

NOL(FileName$)

Description
 


This function will return number of lines from FileName$.

If file doesn't exist NOL returns zero.

Code Examples
 

OpenFile("txt Files (*.txt)|*.txt|All Files|*.*||","*.txt")
If (OpenFile$<>'') Then
  RetVal=NOL(OpenFile$)
End

Result of the above code will be number of lines in selected file. If none file will be selected then NOL function will not be performed.

 

StrCopy(String$, Indx, Count)

Description
 


Returns a string containing Count characters starting with at String$[Index].

If Index is larger than the length of String$, Copy returns an empty string.

If Count specifies more characters than are available, then only the
characters from String$[Index] to the end of String$ are returned.

Code Examples
 

string$='Have a nice day!'
ReturnExt$=StrCopy(string$,1,4)

    Returns Have

This more advanced StrCopy example will show you how to make a simple text typing animation. Simply insert this code to a button and insert new Text object into project.

string$='Have a nice day!'
** count number of letters
count
=LEN(string$)
** create loop from 1 to number of letters
For i=1 to count
** copy one character per loop
  
ReturnExt$=StrCopy(string$,i,1)
** pause loop
  
Pause("100")
** merge letters (this will produce animation effect)
  newstring$=newstring$ +
ReturnExt$
** load merged string to a Text object
  
LoadText("Text","newstring$")
Next i

 

StrDel(String$, Indx, Count)

Description
 


Removes a substring of Count characters from string String starting at String[Index].

If Index is larger than the length of String, no characters are deleted.

If Count specifies more characters than remain starting at the String[Index], StrDel removes the rest of the string and Returns a modified string.

Code Examples
 

string$='Have a nice day!'
ReturnExt$=StrDel(string$,1,5)

    Returns a nice day!

This more advanced StrDel example will show you how to separate Total and Free memory obtained as a single string from GetMemory function.

RetMem$=GetMemory()
separ$='/'
**Get Total memory
n=
POS(separ$,RetMem$)-1
TotalMem$=
StrCopy(RetMem$,0,n)
n=n+1
**Get free memory
FreeMem$=
StrDel(RetMem$,0,n)

 

StrIns(SourceStr$, DestStrs$, Indx)

Description
 


Merges DestStr$ into SourceStr$ at the position S[index]. Returns a modified string.

Code Examples
 

sourcestr$='Have a nice day!'
deststr$=' too'
** count number of letters
count
=LEN(sourcestr$)
ReturnStr$=StrIns(sourcestr$,deststr$,count)

    Returns Have a nice day too!

 

StrGet(String$,Int)

Description
 


Returns the I-th character in String$.

Code Examples
 

string$='Have a nice day!'
count=LEN(string$)
ReturnStr$=StrGet(string$,count)

    Returns !

 

StrSet(String$, Int,C$)

Description
 


Set the I-th character in String$ to character C$. Returns a modified string.

Code Examples
 

string$='Have a nice day!'
count=LEN(string$)
c$=
'!!!'
ReturnStr$=StrSet(string$,count,c$)

    Returns Have a nice day!!!

 

StrOfChar(C$, Int)

Description
 


Returns a string of length I with all characters set to character C$.

Code Examples
 

c$='#'
ReturnStr$=StrOfChar(c$,5)

    Returns #####

** use CHR for obtaining special characters from ASCII table
c$=
CHR(177)
ReturnStr$=StrOfChar(c$,5)

    Returns ▒▒▒▒▒

 

StrChange(String$, FromStr$, ToStr$)

Description
 


Change all occurances of FromStr$ in String$ to ToStr$. Returns a modified string.

Code Examples
 

string$='Lord of the Rings'
fromstr$='Rings'
tostr$='Beer'
ReturnStr$=StrChange(string$,fromstr$,tostr$)

    Returns Lord of the Beer

 

StrToFile(FileName$, String$, Append, LineFeed)

Description
 


Saves or appends the specified String$ to the specified file. Returns 1 if succesful, 0 otherwise.
If Append is True (or 1) and the specified file does not exist, a new file is created.
If Append is True (or 1) and the specified file exist, the String$ will append right after last character in the specified file.
If LineFeed and Append are True (or 1), then the specified string will append on next line.
If LineFeed and Append are False (or 0), then the specified string will saved in new file or overwrite the existing one.

Code Examples
 

file$='c:\temp\test.txt'
string$= 'this string will be append to text file'
ReturnVal=StrToFile(file$,string$,TRUE,FALSE)

    This save string$ to test.txt

file$='c:\temp\test.txt'
For i=1 To 10
  string$= 'Line ' + CHAR(i)
  
ReturnVal=StrToFile(file$,string$,TRUE,TRUE)
Next i

    This save 10 lines (Line 1...Line 10) to test.txt

 

StrFromFile(FileName$, FromLine, NumOfLines)

Description
 


This will load the entire file FileName$ (or just single line) to string variable. FromLine is the number of line from which will be file loaded and NumOfLines is a number of lines that will be loaded. If NumOfLines=-1, it will load entire file.  If NumOfLines=-1 and FromLine>0 then it will load rest of file fromthe line determined by FromLine

Code Examples
 

file$='c:\temp\test.txt'
fromline= 1
numoflines=10
ReturnStr$=StrFromFile(file$,fromline,numoflines)

    Returns first 10 lines from test.txt

 

ExtractExt(FileName$)
ExtractDir(FileName$)
ExtractName(FileName$)
ExtractDrive(FileName$)

Description
 


ExtractExt(FileName$)
Extracts the extension part of the given file name (path).

ExtractDir(FileName$)

Extracts the drive and directory parts of the given path.

 

ExtractName(FileName$)

Extracts the name and extension parts of the given file name (path).

 

ExtractDrive(FileName$)

Returns a string containing the 'drive' portion of a fully qualified path name for the file passed in the FileName (path).

Code Examples
 

Path$='c:\MyFiles\text.txt'
ReturnExt$=ExtractExt(Path$)

    Returns .txt

ReturnDir$=ExtractDir(Path$)

    Returns \MyFiles\

ReturnName$=ExtractName(Path$)

    Returns text

ReturnDrive$=ExtractDrive(Path$)

    Returns c:

MMB Scripting Unleashed by Bokzy, 2003 :: All rights reserved :: http://www.bokzy.com